<template>
	<view class="tabs-wrap adup-route-bz">
		<u-form :model="fm.ps" :ref="fm.fname" :rules="rules" :error-type="['toast']">
			<view class="my-fm" :style="{'padding-top': '0rpx'}">
				<view class="f-item no-box" :style="{'padding-top': '85rpx'}">
					<view class="f-item-title"
						:style="{'position': 'fixed', 'top': '-4rpx', 'width': 'calc(100% - 50rpx)', 'padding-left': '20rpx'}">
						<label class="r-l-icon" :style="{'background': '#ff6300', 'left': '0rpx'}"></label>
						合并订单
						<view class="r-r-con">

							<!-- <u-button :hover-class="'none'" :custom-style="fm.sb.cancleBtnSy" @click="btn_car()">
								线下派车
							</u-button> -->
							<!-- <u-button :ripple="true" :hover-class="'none'" :custom-style="fm.sb.okBtnSty"
								:loading="fm.sb.load" @click="btn_car3()">
								线上派车
							</u-button> -->
							<u-button :ripple="true" :hover-class="'none'" :custom-style="fm.sb.okBtnSty"
								:loading="fm.sb.load" @click="btn_car3()">
								合并订单
							</u-button>
						</view>
					</view>
					<view style="height: 200rpx;">
						<view style="display: flex;justify-content: space-between;align-items: center;"
							v-for="(it, index) in data_list">
							<view
								style="height: 50rpx;width:75vw; white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">
								{{index+1}}:【{{it.personNum}}人】{{it.details}}

							</view>
							<view v-if='index != 0' style="font-weight: 700;padding: 0rpx 20rpx;height: 50rpx;color:red"
								@click="clear_btn(index)">删除</view>
						</view>
					</view>

					<u-divider color="red">已合并订单</u-divider>

					<u-form-item>
						<view class="my-list"
							style="height:1000rpx;width: 100%;overflow: auto;min-height: 1000rpx;background-color: #EFEFEF; ">
							<template v-if="findComboCusList.length>0">
								<view style="" class="ml-item" v-for="(it, index) in findComboCusList" :key="it.id">
									<view class="i-title-row">
										<view class="r-title" style="color: red;">{{it.routeSimpleName}}</view>
										<view class="r-detail-btn" @click="showBzDetail(it.id)">详情
											<u-icon name="arrow-right" color="#666"></u-icon>
										</view>

									</view>

									<view class="content">
										<view class="title" v-html="it.details"></view>
									</view>
									<view style="font-weight: 700;">
										<!-- {{it.cusName+'-'+it.cusMobile}} -->
										<view style='color: #4749a0;'>客户信息:{{it.cusInfo}}
											<text style="color: red;">({{it.personNum}}人)</text>
										</view>
									</view>
									<view class="bottom">
										<view v-if="data_list.length <4"
											:style="{'border-color': '#f00000', 'color': '#f00000'}" class="btn"
											@click="btn_car2(it)">
											合并订单
										</view>
										<!-- <view :style="{'border-color': '#008000', 'color': '#008000'}" class="btn"
											@click="">
											线下派车
										</view>
										<view :style="{'border-color': '#5500ff', 'color': '#5500ff'}" class="btn"
											@click="">
											线上派车
										</view> -->
									</view>

								</view>

							</template>
							<template v-if="findComboCusList.length==0">
								<u-divider color="red">没有数据</u-divider>
							</template>
						</view>
					</u-form-item>



				</view>
			</view>
		</u-form>
		<u-modal show-cancel-button="true" v-model="show2" :content="'确认合并订单?'" @confirm="ok_Btn2"></u-modal>
	</view>
</template>

<script>
	export default {
		name: "offline-vehicle-dispatch",


		data() {
			return {
				data_list: [],
				objdata: {},
				objlist: {},
				show: false,
				show2: false,
				findComboCusList: [],
				options1: [],
				options2: [{
						label: '没有驾驶员',
						value: 0,
					},
					{
						label: '有驾驶员',
						value: 1,
					},
				],
				options3: [{
						label: '月用金(工资)升序',
						value: 0,
					},
					{
						label: '评分最高降序',
						value: 1,
					},
					{
						label: '最快达到',
						value: 2,
					},
				],
				id: '',
				type:'0',
				// 维修详情输入框样式
				inpBtnStyl: {
					'color': '#00C3CE',
					'padding-right': '30rpx'
				},

				// 表单对象
				fm: {
					fname: 'offlinevehicledispatch',
					sb: {
						load: false,
						text: '添加',
						okBtnSty: {
							'background-color': '#00C3CE',
							'color': '#FFFFFF',
							'margin-left': '10rpx',
							'height': '66rpx',
							'font-size': '30rpx',
							'padding': '0 20rpx',
						},
						cancleBtnSy: {
							'color': '#ff6300',
							'margin-left': '10rpx',
							'height': '66rpx',
							'font-size': '30rpx',
							'padding': '0 20rpx',
						}
					},
					// 参数对象
					ps: {
						carcompen: '',
						carName: '',
						carphone: '',

						ids: '',
						carCompanyId: '',
						carDutyUserId: '',
						driverName: '',
						driverPhone: '',
						outPrice: '',
						plateNum: '',

					},
					find: {
						id: '',
						notContainOrderId: [],
					},

				},


			}
		},




		onLoad(e) {
			
			let that = this
			that.data_list = []
			that.id = e.id
			that.type = e.type
			that.fm.find.id = e.id
			let ps = that.fm.find
			ps.notContainOrderId = []
			that.$u.api.getOrderDetail({
				id: e.id
			}).then(res => {
				if (res.code == 1) {
					that.data_list.push(res.data)
					ps.notContainOrderId.push(e.id)
					that.$u.api.mergeQuery(ps).then(res => {
						if (res.code == 1) {
							that.findComboCusList = res.data
						}
					})
				}
			});


		},
		methods: {
			// 关闭弹框
			closeMd() {
				uni.navigateBack({
					delta: 1
				});
			},
			showBzDetail(e) {
				uni.navigateTo({
					url: '/order-man/order-detail/order-detail?id=' + e
				})
			},
			clear_btn(e) {
				let that = this
				that.data_list.splice(e, 1);

				let ps = that.fm.find
				ps.notContainOrderId.splice(e, 1);
				that.$u.api.mergeQuery(ps).then(res => {
					if (res.code == 1) {
						that.findComboCusList = res.data
					}
				})
			},
			btn_car2(e) {
				let that = this
				that.objlist = e
				that.show2 = true
			},
			ok_Btn2() {
				let that = this
				that.data_list.push(that.objlist)

				let ps = that.fm.find
				ps.notContainOrderId.push(that.objlist.id)
				that.$u.api.mergeQuery(ps).then(res => {
					if (res.code == 1) {
						that.findComboCusList = res.data
					} else {
						that.findComboCusList = []
					}
				})

			},
			btn_car(e) {
				let that = this
				let ps = that.fm.find
				uni.removeStorageSync('carCompanyId');

				let id = ps.notContainOrderId.join(',');

				uni.navigateTo({
					url: '/order-man/charter-a-car/received/offline-vehicle-dispatch?id=' + id
				})
			},
			btn_car3(e) {
				let that = this
				let ps = that.fm.find


				let id = ps.notContainOrderId.join(',');
				let needSeats = 1
				for (let i = 0; i < that.data_list.length; i++) {
					needSeats = needSeats + that.data_list[i].personNum
				}
				// uni.navigateTo({
				// 	url: '/order-man/charter-a-car/received/online-vehicle-dispatch?id=' + id + '&seats=' +
				// 		needSeats + '&type=' + that.type
				// })
				let ids = ps.notContainOrderId
				let startTime = '' 
				for (let i = 0; i < that.data_list.length; i++) {
					if (startTime) {
						let time1 = new Date(startTime).getTime()
						let time2 = new Date(that.data_list[i].startTime).getTime()
						if (time1 > time2) {
							startTime = that.data_list[i].startTime
						}
					
					} else {
						startTime = that.data_list[i].startTime
					}
				}
				
				
				that.$u.api.merge({
					ids: ids,
					startTime:startTime,
				}).then(res => {
					if (res.code == 1) {
						if(that.type == 0){
							let tabs_page = {
								tabs1: 2,
								tabs2: 1,
								tabs3: 1,// 拼车接天府
							}
							uni.setStorageSync('tabs_page', tabs_page);
						}else{
							let tabs_page = {
								tabs1: 2,
								tabs2: 2,
								tabs3: 1,// 拼车接天府
							}
							uni.setStorageSync('tabs_page', tabs_page);
						}
						
				
						let timer = null
						uni.$u.toast(res.msg);
						clearTimeout(timer)
						timer = setTimeout(() => {
							uni.reLaunch({
								url: '/pages/tabbar/order-list/order-list'
							});
							clearTimeout(timer)
						}, 1000)
				
					} else {
						uni.showToast({
							icon:'none',
							title:res.msg,
							duration: 2000
						});
						
					}
				})
				
			},
			ok_Btn() {
				uni.navigateTo({
					url: '/order-man/order-detail/order-detail?id=' + e
				})
			},
		}
	}
</script>

<style lang="scss">
	.bottom {
		display: flex;
		margin-top: 20rpx;
		justify-content: flex-end;
		align-items: center;

		.btn {
			line-height: 52rpx;
			padding: 0rpx 16rpx;
			border-radius: 26rpx;
			border: 2rpx solid #ff9900;
			font-size: 26rpx;
			text-align: center;
			color: #ff9900;
			;
			margin-right: 10rpx;
		}

		.more {
			align-self: center;
			flex: 1;
			margin-right: 10rpx;
		}

		.evaluate {
			color: $u-type-warning-dark;
			border-color: $u-type-warning-dark;
		}
	}
</style>